Ext.define('Ext.ux.CKEditor', {
    extend: 'Ext.form.field.TextArea',
    alias: 'widget.ckeditor',
    initComponent: function() {
        this.callParent(arguments);
        this.on('afterrender', function() {
            this.editor = CKEDITOR.replace(this.inputEl.id,
                this.CKConfig);
            var _config = {};
            if (this.ex_style != 'full') {
                _config.toolbar = [{
                    name: 'basic',
                    items: ['Maximize', 'Source', '-', 'Preview', 'Bold', 'Italic', 'Underline', 'Strike', 'Link', 'Image', 'Table', 'Styles', 'Format', 'Font', 'FontSize', 'lineheight']
                }];
            }
            Ext.apply(this.CKConfig, _config);

            this.editorId = this.editor.id;
            // console.log(this);

        }, this);
    },
    onRender: function(ct, position) {
        if (!this.el) {
            this.defaultAutoCreate = {
                tag: 'textarea',
                autocomplete: 'off'
            };
        }
        this.callParent(arguments)
    },

    setValue: function(value) {
        this.callParent(arguments);
        if (this.editor) {
            this.editor.setData(value);
        }
    },
    getRawValue: function() {
        if (this.editor) {
            return this.editor.getData()
        } else {
            return ''
        }
    }

});
CKEDITOR.on('instanceReady', function(e) {
    var o = Ext.ComponentQuery.query('ckeditor[editorId="' + e.editor.id + '"]');
    var h = 600;
    var w = '100%';
    if (o && o[0] && o[0].height){
        h = o[0].height;
    }
    // var h = o[0].getHeight() || 600;
    var td = document.getElementById('cke_contents_' + e.editor.name),
        tbody = td.parentNode.parentNode;
    td.style.height = h - tbody.rows[0].offsetHeight - tbody.rows[2].offsetHeight + 'px';
    td.style.width = w; 
    // var o = Ext.ComponentQuery.query('ckeditor[editorId="' + e.editor.id + '"]'),
    //     comp = o[0];
    // console.log(comp);
    // e.editor.resize(comp.getWidth(), comp.getHeight());
    // comp.on('resize', function(c, adjWidth, adjHeight) {
    //     console.log(adjWidth);
    //     console.log(adjHeight);
    //     c.editor.resize(adjWidth, adjHeight)
    // })
});
/**
 * Created with JetBrains WebStorm.
 * User: huwei
 * Date: 13-8-1
 * Time: 下午2:26
 * To change this template use File | Settings | File Templates.
 */